'use strict';

const Controller = require('egg').Controller;

const uuid = require('node-uuid'); //生成唯一的标识符

class OrderController extends Controller {
    async addOrder() {
        let {ctx,service} = this;
        // [{},{}]  ----JSON.stringify---->  字符串

        let products = JSON.parse(ctx.request.body.products);  //[]
        //下单的人员
        let user = ctx.info.username; 
        //orderId  订单id

        let orderId  = uuid.v1(); //基于时间戳来生成的订单id

        try{
            await service.order.addOrder(products,user,orderId);

            ctx.body = {
                code:1,
                msg:'订单提交成功'
            }
        }catch(e){
            ctx.body = {
                code:0,
                msg:e
            }
        }
    }

    async orderList(){
        let {ctx,service} = this;
        let {username} = ctx.info;
        let orderList = await service.order.orderList(username);
        ctx.body = {
            code:1,
            data:orderList
        }
    }
}

module.exports = OrderController;
